﻿
using PiControls.Controls.Switch;
using PiFramework;
using PiUi.View;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace PiUi.Froms
{
    public partial class ConfigView : BaseView
    {
        private string m_dataSouPaht = string.Empty;
        private string m_dataSavePath = string.Empty;
        private bool m_isSaveOKSou = true;
        private bool m_isSaveNGSou = true;
        private bool m_isSaveOKRes = true;
        private bool m_isSaveNGRes = true;

        private int m_ThreadSpeed   = 400;

        public ConfigView()
        {
            InitializeComponent();
        }
        public override void LoadTheme()
        {

            foreach (Control ctrl in GetAllControls(this))
            {
                if (ctrl.GetType() == typeof(Button))
                {
                    Button btn = (Button)ctrl;
                    btn.BackColor = ThemeColor.PrimaryColor;
                    btn.ForeColor = Color.White;
                    btn.FlatAppearance.BorderColor = ThemeColor.SecondaryColor;
                }

                if (ctrl.GetType() == typeof(GroupBox))
                {
                    GroupBox L_ctrl = (GroupBox)ctrl;
                    L_ctrl.ForeColor = ThemeColor.PrimaryColor;
             
                }
                if (ctrl.GetType() == typeof(Label))
                {
                    Label L_ctrl = (Label)ctrl;
                    L_ctrl.ForeColor = Color.Black;

                }

                if (ctrl.GetType() == typeof(PiSwitch))
                {

                    PiSwitch L_ctrl = (PiSwitch)ctrl;
                    L_ctrl.L_OpenColor = ThemeColor.PrimaryColor; 
                }

            }

            updataToUI();
        }


        private IEnumerable<Control> GetAllControls(Control control)
        {
            foreach (Control child in control.Controls)
            {
                // 返回当前子控件
                yield return child;
                // 递归返回子控件的子控件
                foreach (Control descendant in GetAllControls(child))
                {
                    yield return descendant;
                }
            }
        }



        private void buttonSelectSouPath_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
            {
                folderDialog.Description = "请选择一个文件夹路径";
                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    string selectedPath = folderDialog.SelectedPath;
                    m_dataSouPaht = selectedPath;
                    this.textBoxSouPath.Text = m_dataSouPaht;
                    PSysConfigVar.Instance.SetConfigVar("dataSouPaht", m_dataSouPaht);
                }
            }
        }

        private void buttonSaveConfig_Click(object sender, EventArgs e)
        {
            m_ThreadSpeed = (int)this.numericUpDownThreadSpeed.Value;
            PSysConfigVar.Instance.SetConfigVar("ThreadSpeed", m_ThreadSpeed);
            PSysConfigVar.Instance.SavePSysConfigVarToJson();
            MessageBox.Show("保存成功！");
        }

        private void buttonSelectSavePath_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
            {
                folderDialog.Description = "请选择一个文件夹路径";
                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    string selectedPath = folderDialog.SelectedPath;
                    m_dataSavePath = selectedPath;
                    this.textBoxSavePath.Text = m_dataSavePath;
                    PSysConfigVar.Instance.SetConfigVar("dataSavePaht", m_dataSavePath);

                }
            }
        }

        private void piSwitchOkSuo_LSwitched(object sender, DemoControls.LEventArgs e)
        {
            m_isSaveOKSou = (bool)e.Value;
            PSysConfigVar.Instance.SetConfigVar("isSaveOKSou", m_isSaveOKSou);
        }

        private void piSwitchNgSuo_LSwitched(object sender, DemoControls.LEventArgs e)
        {
            m_isSaveNGSou = (bool)e.Value;
            PSysConfigVar.Instance.SetConfigVar("isSaveNGSou", m_isSaveNGSou);
        }

        private void piSwitchOkRes_LSwitched(object sender, DemoControls.LEventArgs e)
        {
            m_isSaveOKRes = (bool)e.Value;
            PSysConfigVar.Instance.SetConfigVar("isSaveOKRes", m_isSaveOKRes);
        }

        private void piSwitchNgRes_LSwitched(object sender, DemoControls.LEventArgs e)
        {
            m_isSaveNGRes = (bool)e.Value;
            PSysConfigVar.Instance.SetConfigVar("isSaveNGRes", m_isSaveNGRes);
        }

        private void ConfigView_Load(object sender, EventArgs e)
        {


            updataToUI();

        }


        private void updataToUI() {

            m_dataSouPaht = PSysConfigVar.Instance.GetConfigVar("dataSouPaht") == null ? string.Empty : (string)PSysConfigVar.Instance.GetConfigVar("dataSouPaht");
            m_dataSavePath = PSysConfigVar.Instance.GetConfigVar("dataSavePaht") == null ? string.Empty : (string)PSysConfigVar.Instance.GetConfigVar("dataSavePaht");
            m_isSaveOKSou = PSysConfigVar.Instance.GetConfigVar("isSaveOKSou") == null ? true : (bool)PSysConfigVar.Instance.GetConfigVar("isSaveOKSou");
            m_isSaveNGSou = PSysConfigVar.Instance.GetConfigVar("isSaveNGSou") == null ? true : (bool)PSysConfigVar.Instance.GetConfigVar("isSaveNGSou");
            m_isSaveOKRes = PSysConfigVar.Instance.GetConfigVar("isSaveOKRes") == null ? true : (bool)PSysConfigVar.Instance.GetConfigVar("isSaveOKRes");
            m_isSaveNGRes = PSysConfigVar.Instance.GetConfigVar("isSaveNGRes") == null ? true : (bool)PSysConfigVar.Instance.GetConfigVar("isSaveNGRes");
            m_ThreadSpeed = PSysConfigVar.Instance.GetConfigVar("ThreadSpeed") == null ? 400 : (int)PSysConfigVar.Instance.GetConfigVar("ThreadSpeed");

  

            this.textBoxSouPath.Text = m_dataSouPaht;
            this.textBoxSavePath.Text = m_dataSavePath;
            this.piSwitchOkSuo.L_IsOpen = m_isSaveOKSou;
            this.piSwitchNgSuo.L_IsOpen = m_isSaveNGSou;
            this.piSwitchOkRes.L_IsOpen = m_isSaveOKRes;
            this.piSwitchNgRes.L_IsOpen = m_isSaveNGRes;
            this.numericUpDownThreadSpeed.Value =  m_ThreadSpeed;

          

        }

     
    }
}
